package com.system.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.am.base.BaseDao;
import com.system.uitls.CommonUtil;
import com.system.uitls.IdUtils;

public class DoctorDao extends BaseDao
{
  public List<Map<String, Object>> getDiagnosisListNow(String userId, String campus)
  {
    String sql = "SELECT person_name,person_id,id,status FROM sys_patient_record WHERE (status=1 OR (status=2 AND user_id=?)) AND person_campus=?";
    return getSimpleJdbc().queryForList(sql, new Object[] { userId, campus });
  }

  public Map<String, Object> getWaitForDiagnoPerson(String record_id, String user_id, String user_name)
  {
    Map result = null;
    String sql = "";
    try {
      sql = "SELECT status FROM sys_patient_record WHERE id=?";
      result = getSimpleJdbc().queryForMap(sql, new Object[] { record_id });

      if ("2".equals(result.get("status")))
      {
        result.put("repeat", "true");
        return result;
      }

      sql = "UPDATE sys_patient_record SET status=?,user_id=?,user_name=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { "2", user_id, user_name, record_id });
    }
    catch (Exception ex)
    {
      logger.info("DoctorDao[getWaitForDiagnoPerson] sql:" + sql + "\n" + ex.getMessage());
    }
    return result;
  }

  public Map<String, String> injection(Map<String, Object> map)
  {
    String id = CommonUtil.nullToEmptyString(map.get("id"));
    String create_time = CommonUtil.getCurDate();
    String record_id = CommonUtil.nullToEmptyString(map.get("record_id"));
    String private_drug = CommonUtil.nullToEmptyString(map.get("private_drug"));
    String notice = CommonUtil.nullToEmptyString(map.get("notice"));
    String other = CommonUtil.nullToEmptyString(map.get("other"));

    Map result = new HashMap();
    String sql = "";

    if ("".equals(id))
      try
      {
        id = IdUtils.getSequence(getJdbc(), "sys_infuse_id");
        sql = "INSERT INTO sys_infuse(id,create_time,notice,other,private_drug,record_id) VALUES(?,?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { id, create_time, notice, other, private_drug, record_id });

        result.put("type", "success");
        result.put("value", "保存成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[injection] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "保存失败");
      }
    else {
      try {
        sql = "update sys_infuse set notice=?,other=?,private_drug=? WHERE id=?";
        getSimpleJdbc().update(sql, new Object[] { notice, other, private_drug, id });
        result.put("type", "success");
        result.put("value", "保存成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[injection] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "保存失败");
      }
    }
    return result;
  }

  public Map<String, String> diseaseLeave(Map<String, Object> map)
  {
    String diseaseLeaveId = CommonUtil.nullToEmptyString(map.get("diseaseLeaveId"));
    String create_time = CommonUtil.nullToEmptyString(map.get("create_time"));
    String record_id = CommonUtil.nullToEmptyString(map.get("record_id"));
    String sick = CommonUtil.nullToEmptyString(map.get("sick"));
    String days = CommonUtil.nullToEmptyString(map.get("days"));

    Map result = new HashMap();
    String sql = "";

    if ("".equals(diseaseLeaveId))
      try
      {
        diseaseLeaveId = IdUtils.getSequence(getJdbc(), "sys_diseaseLeave_id");
        sql = "INSERT INTO sys_diseaseLeave(id,create_time,sick,days,record_id) VALUES(?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { diseaseLeaveId, create_time, sick, days, record_id });

        result.put("type", "success");
        result.put("value", "申请病假成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[diseaseLeave] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "申请病假失败");
      }
    else {
      try {
        sql = "update sys_diseaseLeave set create_time=?,sick=?,days=? WHERE id=?";
        getSimpleJdbc().update(sql, new Object[] { create_time, sick, days, diseaseLeaveId });
        result.put("type", "success");
        result.put("value", "申请病假成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[diseaseLeave] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "申请病假失败");
      }
    }
    return result;
  }

  public Map<String, String> referral(Map<String, Object> map)
  {
    String referral_id = CommonUtil.nullToEmptyString(map.get("referral_id"));
    String create_time = CommonUtil.nullToEmptyString(map.get("create_time"));
    String record_id = CommonUtil.nullToEmptyString(map.get("record_id"));
    String hospital = CommonUtil.nullToEmptyString(map.get("hospital"));
    String subject = CommonUtil.nullToEmptyString(map.get("subject"));
    String sick = CommonUtil.nullToEmptyString(map.get("sick"));

    Map result = new HashMap();
    String sql = "";

    if ("".equals(referral_id))
      try
      {
        referral_id = IdUtils.getSequence(getJdbc(), "sys_diseaseLeave_id");
        sql = "INSERT INTO sys_referral(id,create_time,hospital,subject,record_id,sick) VALUES(?,?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { referral_id, create_time, hospital, subject, record_id, sick });

        result.put("id", referral_id);
        result.put("type", "success");
        result.put("value", "申请转诊成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[referral] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "申请转诊失败");
      }
    else {
      try {
        sql = "update sys_referral set create_time=?,hospital=?,subject=?,record_id=?,sick=? WHERE id=?";
        getSimpleJdbc().update(sql, new Object[] { create_time, hospital, subject, record_id, sick, referral_id });
        result.put("type", "success");
        result.put("value", "申请转诊成功");
      }
      catch (Exception ex) {
        logger.info("DoctorDao[referral] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "申请转诊失败");
      }
    }
    return result;
  }

  public Map<String, String> dressing(Map<String, Object> map)
  {
    String dressingId = CommonUtil.nullToEmptyString(map.get("dressingId"));
    String record_id = CommonUtil.nullToEmptyString(map.get("record_id"));
    String create_time = CommonUtil.getFormatCurDate("yyyy-MM-dd");
    String place = CommonUtil.nullToEmptyString(map.get("place"));
    String scope = CommonUtil.nullToEmptyString(map.get("scope"));
    String[] method = (String[])map.get("method");
    String times = CommonUtil.nullToEmptyString(map.get("times"));
    String time = CommonUtil.nullToEmptyString(map.get("time"));
    String days = CommonUtil.nullToEmptyString(map.get("days"));

    StringBuffer methodIds = new StringBuffer();
    for (int i = 0; (method != null) && (i < method.length); i++)
    {
      methodIds.append(method[i]).append(",");
    }

    if (methodIds.length() >= 1)
    {
      methodIds.deleteCharAt(methodIds.length() - 1);
    }

    Map result = new HashMap();
    String sql = "";

    if ("".equals(dressingId))
      try
      {
        dressingId = IdUtils.getSequence(getJdbc(), "sys_dressing_id");
        sql = "INSERT INTO sys_dressing(id,place,scope,method,create_time,record_id,times,days,time) VALUES(?,?,?,?,?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { dressingId, place, scope, methodIds, create_time, record_id, times, days, time });
        result.put("id", dressingId);
        result.put("type", "success");
        result.put("value", "换药成功");
      }
      catch (Exception ex) {
        logger.error("DoctorDao[dressing] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "换药失败");
      }
    else {
      try
      {
        sql = "update sys_dressing set place=?,scope=?,method=?,record_id=?,times=?,days=?,time=? WHERE id=?";
        getSimpleJdbc().update(sql, new Object[] { place, scope, methodIds, record_id, times, days, time, dressingId });
        result.put("type", "success");
        result.put("value", "换药成功");
      }
      catch (Exception ex) {
        logger.error("DoctorDao[dressing] sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "换药失败");
      }
    }
    return result;
  }

  public Map<String, Object> saveDiagnose(List<Map<String, Object>> list, Map<String, Object> map)
  {
    String diagnoseId = CommonUtil.nullToEmptyString(map.get("diagnoseId"));
    String description = CommonUtil.nullToEmptyString(map.get("description"));
    String symptom = CommonUtil.nullToEmptyString(map.get("symptom"));
    String post_number = CommonUtil.nullToEmptyString(map.get("post_number"));
    if ("".equals(post_number))
    {
      post_number = null;
    }
    String recordId = CommonUtil.nullToEmptyString(map.get("recordId"));
    String currentTime = CommonUtil.getCurDate();

    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    try
    {
      if ("".equals(diagnoseId))
      {
        diagnoseId = IdUtils.getSequence(getJdbc(), "sys_diagnose_id");
        sqlList.add("INSERT INTO sys_diagnose(id,create_time,description,symptom,post_number,record_id) VALUES(?,?,?,?,?,?)");
        params.add(new Object[] { diagnoseId, currentTime, description, symptom, post_number, recordId });
      }
      else {
        sqlList.add("UPDATE sys_diagnose SET description=?,symptom=?,post_number=? WHERE id=?");
        params.add(new Object[] { description, symptom, post_number, diagnoseId });
      }

      sqlList.add("DELETE FROM sys_prescription WHERE diagnose_id=?");
      params.add(new Object[] { diagnoseId });

      for (Map obj : list)
      {
        String drugId = CommonUtil.nullToEmptyString(obj.get("drugId"));
        String drug_type = CommonUtil.nullToEmptyString(obj.get("drug_type"));
        int quantity = CommonUtil.convertToInt(CommonUtil.nullToEmptyString(obj.get("quantity")), 0);
        Map m = getSimpleJdbc().queryForMap("SELECT supply_price FROM sys_drug WHERE id=?", new Object[] { drugId });
        float supply_price = Float.parseFloat(CommonUtil.nullToEmptyString(m.get("supply_price")));
        float amount = supply_price * quantity;
        String prescription_id = IdUtils.getSequence(getJdbc(), "sys_prescription_id");
        sqlList.add("INSERT INTO sys_prescription(id,drug_id,quantity,use_method,times,amount,diagnose_id,type,consumption) VALUES(?,?,?,?,?,?,?,?,?)");
        params.add(new Object[] { prescription_id, drugId, obj.get("quantity"), obj.get("method"), obj.get("times"), Float.valueOf(amount), diagnoseId, drug_type, obj.get("consumption") });
      }
      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "诊断成功");
      logger.info("DoctorDao[saveDiagnose] sql:" + sqlList.toString());
    } catch (Exception ex) {
      logger.error("DoctorDao[saveDiagnose] sql:" + sqlList.toString() + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "诊断失败");
    }

    return result;
  }

  public Map<String, Object> getDiagnoseInfo(String record_id, String person_id)
  {
    Map result = new HashMap();
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(1);
    try
    {
      Map personMap = getSimpleJdbc().queryForMap("SELECT name,sex,t1.type,telephone,anamnesis,class,address,(SELECT max_consume FROM sys_cost_definition t WHERE person_type=t1.type AND t.register_type=t2.register_type) as max_consume FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      personMap.putAll(getSimpleJdbc().queryForMap("SELECT count(1) as times,isnull(sum(amount),0) as amount,convert(char(10),max(create_time)) as last_time FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND DATEPART(yyyy,create_time)=? AND t2.person_id=? AND t2.status='7'", new Object[] { Integer.valueOf(year), person_id }));

      Map diagnoseMap = null;
      List diagnoseList = getSimpleJdbc().queryForList("SELECT id,description,symptom,post_number FROM sys_diagnose WHERE record_id=?", new Object[] { record_id });
      if ((diagnoseList != null) && (diagnoseList.size() == 1))
      {
        diagnoseMap = (Map)diagnoseList.get(0);
      }
      else diagnoseMap = new HashMap(0);

      String diagnose_id = CommonUtil.nullToEmptyString(diagnoseMap.get("id"));
      List list = getSimpleJdbc().queryForList("SELECT t1.drug_id,t2.drugstore_num,t2.name,t2.spec,t2.supply_price,t1.type,t1.quantity,t1.use_method,t1.times,t1.amount,t1.consumption FROM sys_prescription t1,sys_drug t2 WHERE t1.diagnose_id=? AND t1.drug_id=t2.id", new Object[] { diagnose_id });

      result.put("personMap", personMap);
      result.put("diagnoseMap", diagnoseMap);
      result.put("list", list);
    } catch (Exception ex) {
      logger.error("DoctorDao[getDiagnoseInfo]-->查询电子处方信息出错\n" + ex.getMessage());
    }

    return result;
  }

  public Map<String, Object> getDiseaseDetails(String record_id)
  {
    Map result = new HashMap();
    try
    {
      Map personMap = getSimpleJdbc().queryForMap("SELECT name,sex,t1.type,telephone,anamnesis,class,address,t2.user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });

      Map diagnoseMap = null;
      List diagnoseList = getSimpleJdbc().queryForList("SELECT id,description,symptom,post_number FROM sys_diagnose WHERE record_id=?", new Object[] { record_id });
      if ((diagnoseList != null) && (diagnoseList.size() == 1))
      {
        diagnoseMap = (Map)diagnoseList.get(0);
        String[] descriptions = CommonUtil.nullToEmptyString(diagnoseMap.get("description")).split("\\|\\|");
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < descriptions.length; i++)
        {
          str.append(descriptions[i]).append("、");
        }
        str.deleteCharAt(str.length() - 1);

        diagnoseMap.put("description", str);
        StringBuffer target = new StringBuffer();
        String symptom = CommonUtil.nullToEmptyString(diagnoseMap.get("symptom"));
        if (!"".equals(symptom)) {
          List<Map<String, Object>> dictionaryList = getSimpleJdbc().queryForList("SELECT value FROM sys_data_dictionary WHERE id IN (" + symptom + ")", new Object[0]);
          if (dictionaryList != null) {
            for (Map map : dictionaryList) {
              target.append(CommonUtil.nullToEmptyString(map.get("value"))).append("、");
            }
          }
          diagnoseMap.put("symptom", target.substring(0, target.length() - 1));
        }
      }
      else {
        diagnoseMap = new HashMap(0);
      }

      String diagnose_id = CommonUtil.nullToEmptyString(diagnoseMap.get("id"));
      List list = getSimpleJdbc().queryForList("SELECT t1.drug_id,t2.name,t2.spec,t1.quantity,(SELECT value FROM sys_data_dictionary t3 WHERE t3.id=t1.use_method) as use_method,(SELECT value FROM sys_data_dictionary t3 where t3.id=t1.times) as times,t1.amount FROM sys_prescription t1,sys_drug t2 WHERE t1.diagnose_id=? AND t1.drug_id=t2.id", new Object[] { diagnose_id });

      result.put("personMap", personMap);
      result.put("diagnoseMap", diagnoseMap);
      result.put("list", list);
    } catch (Exception ex) {
      logger.error("DoctorDao[getDiseaseDetails]-->查询电子处方信息出错\n" + ex.getMessage());
    }

    return result;
  }

  public String getNumber()
  {
    Calendar c = Calendar.getInstance();
    int currentDay = c.get(5);

    int count = getSimpleJdbc().queryForInt("SELECT count(1) FROM sys_patient_record WHERE DATEPART(dd,create_time)=?", new Object[] { Integer.valueOf(currentDay) });
    return CommonUtil.getFormatCurDate("yyyyMMdd") + (count + 1);
  }

  public Map<String, Object> getDressingInfo(String record_id)
  {
    Map result = null;
    Calendar c = Calendar.getInstance();
    int currentYear = c.get(1);
    try
    {
      result = getSimpleJdbc().queryForMap("SELECT name,class,t1.type,card_num,sex,birthday,user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      result.put("create_time", CommonUtil.getFormatCurDate("yyyy-MM-dd"));
      List list = getSimpleJdbc().queryForList("SELECT id,place,scope,method,times,create_time,days,time FROM sys_dressing WHERE record_id=?", new Object[] { record_id });

      if ((list != null) && (list.size() == 1))
      {
        result.putAll((Map)list.get(0));
      }

      String birthday = CommonUtil.nullToEmptyString(result.get("birthday"));
      if (!"".equals(birthday))
      {
        result.put("age", Integer.valueOf(currentYear - Integer.parseInt(birthday.substring(0, 4))));
      }

      result.put("number", getNumber());
    }
    catch (Exception e)
    {
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, Object> getInjectionInfo(String record_id)
  {
    Map result = new HashMap();
    Calendar c = Calendar.getInstance();
    int currentYear = c.get(1);
    try {
      Map personMap = getSimpleJdbc().queryForMap("SELECT name,t1.address,sex,birthday,user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      personMap.put("currentDate", CommonUtil.getFormatCurDate("yyyy年MM月dd日"));
      String birthday = CommonUtil.nullToEmptyString(personMap.get("birthday"));
      if (!"".equals(birthday))
      {
        personMap.put("age", Integer.valueOf(currentYear - Integer.parseInt(birthday.substring(0, 4))));
      }
      Map diagnoseMap = null;
      List diagnoseList = getSimpleJdbc().queryForList("SELECT id,description FROM sys_diagnose WHERE record_id=?", new Object[] { record_id });
      if ((diagnoseList != null) && (diagnoseList.size() == 1))
      {
        diagnoseMap = (Map)diagnoseList.get(0);
      }
      else diagnoseMap = new HashMap(0);

      Map infuseMap = null;
      List infuseList = getSimpleJdbc().queryForList("SELECT id,notice,other,private_drug FROM sys_infuse WHERE record_id=?", new Object[] { record_id });
      if ((infuseList != null) && (infuseList.size() == 1))
      {
        infuseMap = (Map)infuseList.get(0);
      }
      else infuseMap = new HashMap(0);

      String diagnose_id = CommonUtil.nullToEmptyString(diagnoseMap.get("id"));
      List list = getSimpleJdbc().queryForList("SELECT t2.name,t1.quantity,t2.spec,t1.consumption,(SELECT value FROM sys_data_dictionary t3 WHERE t3.id=t1.use_method) AS use_method,(SELECT value FROM sys_data_dictionary t3 WHERE t3.id=t1.times) AS times FROM sys_prescription t1,sys_drug t2 WHERE t1.diagnose_id=? AND t1.drug_id=t2.id AND t1.type='2'", new Object[] { diagnose_id });
      result.put("personMap", personMap);
      result.put("diagnoseMap", diagnoseMap);
      result.put("infuseMap", infuseMap);
      result.put("list", list);
    } catch (Exception e) {
      logger.error("DoctorDao[getPrescriptionBase]-->查询注射单出错\n" + e.getMessage());
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, Object> getPrescriptionBase(String record_id)
  {
    Map result = new HashMap();
    Calendar c = Calendar.getInstance();
    int currentYear = c.get(1);
    try {
      Map personMap = getSimpleJdbc().queryForMap("SELECT name,class,t1.type,t1.card_num,t1.address,sex,birthday,user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      personMap.put("currentDate", CommonUtil.getFormatCurDate("yyyy年MM月dd日"));

      String birthday = CommonUtil.nullToEmptyString(personMap.get("birthday"));
      if (!"".equals(birthday))
      {
        personMap.put("age", Integer.valueOf(currentYear - Integer.parseInt(birthday.substring(0, 4))));
      }
      Map diagnoseMap = null;
      List diagnoseList = getSimpleJdbc().queryForList("SELECT id,description,post_number FROM sys_diagnose WHERE record_id=?", new Object[] { record_id });
      if ((diagnoseList != null) && (diagnoseList.size() == 1))
      {
        diagnoseMap = (Map)diagnoseList.get(0);
      }
      else diagnoseMap = new HashMap(0);

      String diagnose_id = CommonUtil.nullToEmptyString(diagnoseMap.get("id"));
      List list = getSimpleJdbc().queryForList("SELECT t2.name,t1.type,t1.quantity,t1.consumption,t1.amount,t2.spec,t2.unit,(SELECT value FROM sys_data_dictionary t3 WHERE t3.id=t1.use_method) AS use_method,(SELECT value FROM sys_data_dictionary t3 WHERE t3.id=t1.times) AS times,t1.amount FROM sys_prescription t1,sys_drug t2 WHERE t1.diagnose_id=? AND t1.drug_id=t2.id", new Object[] { diagnose_id });
      personMap.put("number", getNumber());
      result.put("personMap", personMap);
      result.put("diagnoseMap", diagnoseMap);
      result.put("list", list);
    } catch (Exception e) {
      logger.error("DoctorDao[getPrescriptionBase]-->查询处方笺数据错误\n" + e.getMessage());
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, Object> getdiseaseLeaveInfo(String record_id)
  {
    Map result = null;
    try
    {
      result = getSimpleJdbc().queryForMap("SELECT name,class,user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      List list = getSimpleJdbc().queryForList("SELECT id,create_time,sick,days FROM sys_diseaseLeave WHERE record_id=?", new Object[] { record_id });

      if ((list != null) && (list.size() == 1))
      {
        result.putAll((Map)list.get(0));
      }
    } catch (Exception e) {
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, Object> getReferralInfo(String record_id)
  {
    Map result = null;
    try
    {
      result = getSimpleJdbc().queryForMap("SELECT name,class,user_name FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      List list = getSimpleJdbc().queryForList("SELECT id,create_time,sick,hospital,subject FROM sys_referral WHERE record_id=?", new Object[] { record_id });

      if ((list != null) && (list.size() == 1))
      {
        result.putAll((Map)list.get(0));
      }

      result.put("number", getNumber());
    } catch (Exception e) {
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, Object> getPersonInfo(String record_id, String personId)
  {
    Map result = null;
    Calendar c = Calendar.getInstance();
    int currentYear = c.get(1);
    try
    {
      result = getSimpleJdbc().queryForMap("SELECT name,class,t1.telephone,t1.anamnesis,t1.social_security,t1.type,t1.card_num,t1.card_num,t1.address,sex,birthday,user_name,(SELECT max_consume FROM sys_cost_definition t WHERE t.person_type=t1.type AND t.register_type=t2.register_type) as max_consume FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });
      result.putAll(getSimpleJdbc().queryForMap("SELECT count(1) as times,isnull(sum(amount),0) as totalAmount FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND DATEPART(yyyy,create_time)=? AND t2.status='7' AND t2.person_id=?", new Object[] { Integer.valueOf(currentYear), personId }));
    }
    catch (Exception ex) {
      result = new HashMap(0);
    }

    return result;
  }

  public Map<String, String> back(String record_id)
  {
    String diagnose_id;
    try
    {
      diagnose_id = (String)getSimpleJdbc().queryForObject("SELECT id FROM sys_diagnose  WHERE record_id=?", String.class, new Object[] { record_id });
    }
    catch (Exception ex)
    {
      diagnose_id = "";
    }
    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    String sql = null;
    try
    {
      sqlList.add("UPDATE sys_patient_record SET status=? WHERE id=?");
      params.add(new Object[] { "1", record_id });

      sqlList.add("DELETE FROM sys_diseaseLeave WHERE record_id=?");
      params.add(new Object[] { record_id });

      sqlList.add("DELETE FROM sys_referral WHERE record_id=?");
      params.add(new Object[] { record_id });

      sqlList.add("DELETE FROM sys_dressing WHERE record_id=?");
      params.add(new Object[] { record_id });

      sqlList.add("DELETE FROM sys_infuse WHERE record_id=?");
      params.add(new Object[] { record_id });

      sqlList.add("DELETE FROM sys_diagnose WHERE record_id=?");
      params.add(new Object[] { record_id });

      sqlList.add("DELETE FROM sys_prescription WHERE diagnose_id=?");
      params.add(new Object[] { diagnose_id });

      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "返回成功");
    }
    catch (Exception ex) {
      logger.info("DoctorDao[back] sql:" + sql + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "返回失败");
    }
    return result;
  }

  public Map<String, String> submit(String record_id)
  {
    Map result = new HashMap();
    int diagnose = getSimpleJdbc().queryForInt("SELECT count(*) FROM sys_diagnose  WHERE record_id=?", new Object[] { record_id });
    int referral = getSimpleJdbc().queryForInt("SELECT count(*) FROM sys_referral  WHERE record_id=?", new Object[] { record_id });
    int dressing = getSimpleJdbc().queryForInt("SELECT count(*) FROM sys_dressing  WHERE record_id=?", new Object[] { record_id });
    int diseaseLeave = getSimpleJdbc().queryForInt("SELECT count(*) FROM sys_diseaseLeave  WHERE record_id=?", new Object[] { record_id });
    int inject = getSimpleJdbc().queryForInt("SELECT count(*) FROM sys_prescription t,sys_diagnose t1 WHERE t1.id=t.diagnose_id AND t.type='2' AND t1.record_id=?", new Object[] { record_id });

    int status = 0;
    if ((diagnose == 0) && (referral == 0) && (dressing == 0) && (diseaseLeave > 0))
      status = 7;
    else if ((diagnose == 0) && (referral > 0) && (dressing == 0) && (diseaseLeave == 0))
      status = 7;
    else if ((diagnose > 0) && (dressing == 0) && (inject == 0))
      status = 5;
    else {
      status = 3;
    }
    String sql = null;
    try {
      sql = "UPDATE sys_patient_record SET status=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { Integer.valueOf(status), record_id });
      result.put("type", "success");
      result.put("value", "提交成功");
    }
    catch (Exception ex) {
      logger.info("DoctorDao[submit] sql:" + sql + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "提交失败");
    }
    return result;
  }
}